<template>
  <div class="xtx-pagination">
    <a href="javascript:;" :class="{disabled: myCurrentPage ===1}" @click="changePager(myCurrentPage-1)">上一页</a>
    <span v-if=" myCurrentPage > offset+1">...</span>
    <a href="javascript:;" v-for="item in pager.btnArr" :key="item" :class="{active: item === myCurrentPage}" @click="changePager(item)">{{
        item
      }}</a>
    <span v-if="myCurrentPage + offset < pager.pageCount">...</span>
    <a href="javascript:;" :class="{disabled: myCurrentPage === pager.pageCount}" @click="changePager(myCurrentPage+1)">下一页</a>
    <span class="total">共{{ pager.pageCount }}页, {{ total }}条数据</span>
  </div>
</template>
<script>
import { computed, ref, watch } from 'vue'

export default {
  name: 'XtxPagination',
  props: {
    total: {
      type: Number,
      default: 100
    },
    pageSize: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  emits: ['current-page'],
  setup (props, { emit }) {
    // 需要的数据
    // 1.约定按钮的个数5个,如果想要变成动态的就要是响应式数据
    const count = ref(5)
    // 偏移量
    const offset = Math.floor(count.value / 2)
    // 2. 当前显示的页码
    const myCurrentPage = ref(1)
    // 3. 总页数 = 总条数/每页条数 向上取整
    // 总条数
    const myTotal = ref(100)
    //  每页条数
    const myPageSize = ref(10)
    //  其他数据(总页数,起始按钮,结束按钮,按钮数组)依赖上面数据
    const pager = computed(() => {
      // 总页数
      const pageCount = Math.ceil(myTotal.value / myPageSize.value)
      // 按钮个数和当前页码: 起始按钮,结束按钮,按钮数组
      // 1.理想情况下:
      let start = myCurrentPage.value - offset
      let end = start + count.value - 1
      // 2.如果此时start不合理
      if (start < 1) {
        start = 1
        end = (start + count.value - 1) > pageCount ? pageCount : (start + count.value - 1)
      }
      // 3.如果end不可理
      if (end > pageCount) {
        end = pageCount
        start = (end + 1 - count.value) < 1 ? 1 : (end + 1 - count.value)
      }
      // 按钮数组
      const btnArr = []
      for (let i = start; i <= end; i++) {
        btnArr.push(i)
      }
      return {
        pageCount,
        btnArr,
        start,
        end
      }
    })
    // 监听props的变化,更新组件内部数据
    watch(props, () => {
      myTotal.value = props.total
      myPageSize.value = props.pageSize
      myCurrentPage.value = props.currentPage
    }, { immediate: true })

    // 切换分页的函数
    const changePager = (page) => {
      myCurrentPage.value = page
      //  通知父组件
      emit('current-page', page)
    }
    return {
      myCurrentPage,
      pager,
      offset,
      changePager
    }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;

  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;

    &:hover {
      color: @xtxColor;
    }

    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }

    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;

      &:hover {
        color: #333
      }
    }
  }

  > span {
    margin-right: 10px;
  }
  .total{
    font-size: 12px;
    color: #999999;
    text-align: center;
    margin-top: 10px;
  }
}
</style>
